package presenters;

import android.app.Activity;
import android.view.View;
import data.AddItem;
import data.DBInterfaceError;
import data.IModel;
import data.Item;
import data.Session;
import views.IFieldView;

/**
 * Handles logic and button presses for view
 * 
 * @author Kelsey
 */
public class SubmitItemPresenter {

	/** View of SubmitItem */
	private IFieldView myView;

	/** Models of SubmitItem */
	private IModel myModel;

	/** Activity in use */
	private Activity activity;

	/**
	 * Creates a SubmitItemPresenter
	 * 
	 * @param view
	 *            the view of SubmitIem
	 */
	public SubmitItemPresenter(IFieldView view) {
		myView = view;
		activity = (Activity) myView;
	}

	/**
	 * Tries to add an item
	 * 
	 * @param desc
	 *            a description of the item
	 * @param address
	 *            - the address
	 * @param name
	 *            name of the item
	 * @param source
	 *            - the source
	 */
	public void onSubmitClick(String desc, String location, String name,
			String type, String category, View source) {
		Item item = new Item();
		item.setAccountName(((Session) activity.getApplicationContext())
				.getAccountName());
		System.out.println(category + ", " + type);
		item.setCategory(Item.Category.valueOf(category));
		item.setDescription(desc);
		System.out.println("@@location: " + location + ",name: " + name);
		item.setLocation(location);
		item.setName(name);
		// item.setPicture(null);
		item.setType(Item.ItemType.valueOf("Item" + type));
		item.setStatus(Item.Status.Open);

		boolean success = false;

		try {
			success = new AddItem(activity, item).getResult();
		} catch (DBInterfaceError e) {
			e.printStackTrace();
		}

		if (success) {
			myView.advance(source);
		} else {
			myView.showToast("Something went wrong");
		}
	}

	/**
	 * When item submission is canceled
	 * 
	 * @param source
	 *            - the source
	 */
	public void onCancelClick(View source) {
		myView.advance(source);
	}

	/**
	 * hide the soft keyboard
	 */
	public void hideSoftKeyboard() {
		myView.hideSoftKeyboard(activity.getCurrentFocus());
	}

	public IModel getMyModel() {
		return myModel;
	}

	public void setMyModel(IModel myModel) {
		this.myModel = myModel;
	}
}